<template>
    <div>
        <h1 style="color: red;">用药信息列表显示</h1>
        <input type="text" v-model="p.doctorName">医生姓名
        <select @click="officelist" v-model="p.oid">
            <option value="0">请选择</option>
            <option v-for="item in list"  :value="item.id">{{item.officeName}}</option>
        </select>
        <input type="button" value="查询" @click="cha">
        <input type="button" value="新增用药信息" @click="Add">

        <table class="table">
            <thead>
                <tr>
                    <td>
                        <input type="checkbox" v-model="ck" @change="ck_change">
                    </td>
                    <td>编号</td>
                    <td>药品名称</td>
                    <td>科室</td>
                    <td>用药日期</td>
                    <td>病人名称</td>
                    <td>病房号码</td>
                    <td>用药状态</td>
                    <td>配药医生</td>
                    <td>是否删除</td>
                    <td>操作</td>
                </tr>
            </thead>
            <tbody>
                <tr v-for="item in data"> 
                    <td>
                        <input type="checkbox" :value="item.id" v-model="idarray">
                    </td>
                    <td>{{item.id}}</td>
                    <td>{{item.name}}</td>
                    <td>{{item.officeName}}</td>
                    <td>{{item.createTime}}</td>
                    <td>{{item.peoName}}</td>
                    <td>{{item.warName}}</td>
                    <td>{{item.isAction?"已完成":"未完成"}}</td>
                    <td>{{item.doctName}}</td>
                    <td>{{item.isDelete?"已删除":"未删除"}}</td>
                    <td>
                        <input type="button" value="逻辑删除" @click='DeleteIsdelete(item.id)'>
                        <input type="button" value="修改" @click='UpdateEdit(item.id)'>
                    </td>
                </tr>
            </tbody>
        </table>
        共 {{ p.count }} 数，每页 {{ p.size }} 条，共 {{ p.index }} / {{ p.pcount }} 页
        <br>
        <a href="#" @click="Page(1)">首页</a>|
        <a href="#" @click="Page(2)">上一页</a>|
        <a href="#" @click="Page(3)">下一页</a>|
        <a href="#" @click="Page(4)">尾页</a>

        <select @change="SizeChange" v-model="p.size">
            <option value="4">4</option>
            <option value="6">6</option>
            <option value="10">10</option>
        </select>
        <input type="text" v-model="p.index">
        <input type="button" value="确认" @click="IndexChange">
        <input type="button" value="批量修改" @click="UpdateALL">
    </div>
</template>

<script setup lang="ts">
import axios from 'axios';
import { onMounted, ref } from 'vue';
import { useRoute, useRouter } from 'vue-router';
const route=useRoute();
const router=useRouter();
const ck=ref(false)
const idarray=ref([0])

onMounted(()=>{
    GetData();
})

const ck_change=()=>{
    if(ck.value){
        data.value.forEach(element=>{
            idarray.value.push(element.id)
        })
    }
    else{
        idarray.value=[]
    }
}

const UpdateALL=()=>{
    let s=idarray.value.toString();
    if(!confirm("确认要修改妈？")){
        return;
    }
    axios.get("https://localhost:7187/api/Doctor/UpdateIsActionALL",{params:{idstring:s}}).then(res=>{
        if(res.data>0){
            alert('修改成功')
            GetData();
        }
        else{
            alert('修改失败')
        }
    })
}

const DeleteIsdelete=(id:any)=>{
    if(!confirm("确认要删除妈？")){
        return;
    }
    axios.delete("https://localhost:7187/api/Doctor/DeleteDoctorLogic",{params:{id:id}}).then(res=>{
        if(res.data>0){
            alert('删除成功')
            GetData();
        }
        else{
            alert('删除失败')
        }
    })
}

const cha=()=>{
    p.value.index=1;
    GetData();
}
const list=ref([{
    "id": 1,
    "img": "/pic/1.jpg",
    "createTime": "2025-02-18T05:47:12.254",
    "officeName": "内科"
}])
const officelist=()=>{
    axios.get("https://localhost:7187/api/Office/GetOfficeList").then(res=>{
       list.value=res.data;
    })
}


const Page=(v:any)=>{
    if(v==1){
        p.value.index=1;
    }
    if(v==2){
        if(p.value.index>1){
            p.value.index-=1;
        }
    }
    if(v==3){
        if(p.value.index<p.value.pcount){
            p.value.index+=1;
        }
    }
    if(v==4){
        p.value.index=p.value.pcount;
    }

    GetData();
}
const SizeChange=()=>{
    p.value.index=1;
    GetData();
}
const IndexChange=()=>{
    if(p.value.index<p.value.pcount){
        p.value.index=1;
    }
    if(p.value.index*0!=0){
        p.value.index=1;
    }
    GetData();
}

const data=ref([{
    "id": 1,
      "name": "阿莫西林",
      "oid": 1,
      "createTime": "2025-02-18T06:26:56.411",
      "peoName": "张三",
      "warName": "301",
      "isAction": true,
      "doctName": "时宜",
      "isDelete": true,
      "officeName": "内科"
}])
const p=ref({
    index:1,
    size:4,
    doctorName:"",
    oid:0,
    count:0,
    pcount:0,
})
const GetData=()=>{
    axios.get("https://localhost:7187/api/Doctor/GetDoctorPage",{params:p.value}).then(res=>{
        data.value=res.data.data;

        p.value.count=res.data.count;
        p.value.pcount=Math.ceil(p.value.count/p.value.size);
    })
}

const Add=()=>{
    router.push("/AddDoctor");
}
const UpdateEdit=(id:any)=>{
    router.push({ path: '/EditDoctor',query:{id:id}})
}
</script>

<style scoped>

</style>